package com.atguigu.service_ucneter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.UcenterMemberOrderVo;
import com.atguigu.service_ucneter.entity.UcenterMember;
import com.atguigu.service_ucneter.entity.vo.RegisterVo;
import com.atguigu.service_ucneter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author clark
 * @since 2020-07-23
 */
@RestController
@CrossOrigin
@RequestMapping("/educneter/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;


    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member){
        //调用service方法实现登录
        //返回token值,使用jwt生成

        String token = ucenterMemberService.login(member);
        System.out.println(token);
        return R.ok().data("token",token);
    }

    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(id)) {
            return R.error().code(28004).message("请登录");
        }
        UcenterMember member = ucenterMemberService.getById(id);

        return R.ok().data("userInfo",member);
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfoById/{id}")
    public Map getMemberInfoById(@PathVariable("id") String id){
        UcenterMember member = ucenterMemberService.getById(id);
        Map map =new HashMap<>();
        map.put("memberId",member.getId());
        map.put("avatar",member.getAvatar());
        map.put("nickName",member.getNickname());
        return map;
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfoOrderById/{id}")
    public UcenterMemberOrderVo getMemberInfoOrderById(@PathVariable("id") String id){
        UcenterMember member = ucenterMemberService.getById(id);
        UcenterMemberOrderVo ucenterMemberOrderVo = new UcenterMemberOrderVo();
        BeanUtils.copyProperties(member,ucenterMemberOrderVo);
        return ucenterMemberOrderVo;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);
        return R.ok().data("countRegister",count);
    }






}

